<?php
class ArticleController extends Yaf_Controller_Abstract {

   	public function init(){
         if(is_null(Yaf_Session::getInstance()->get(AK47))){
            $this->redirect('/index.php/admin/login/login');
         }
         if ($this->getRequest()->isXmlHttpRequest()) {
            Yaf_Dispatcher::getInstance()->disableView();
         }

   		$this->setViewPath(APP_PATH . '/application/modules/Admin/views');

         $data = array(
               'right_title'        =>'文章管理',
               "title"              => '惠信后台管理',
               'article_li_active'  =>"class='active'",
               'content_nav'        =>array(
                  array('name'=>'文章列表','href'=>'/index.php/admin/article/index'),
                  array('name'=>'添加文章','href'=>'/index.php/admin/article/add'),
                  array('name'=>'分类管理','href'=>'/index.php/admin/article/type'),
                  array('name'=>'添加分类','href'=>'/index.php/admin/article/addtype'),
               )
            );
         $this->getView()->assign($data);
   	}
      /**
       * 文章列表
       * @return [type] [description]
       */
      public function indexAction(){
         $article_model = new articleModel();
         $type_model    = new typeModel();
         $article_list  = $article_model->select(array(),array('addtime'=>1));

         foreach ($article_list as $key => $value) {
            $article_list[$key]['addtime'] = date('Y-m-d',$value['addtime']);

            if($value['status'] == '1'){
               $article_list[$key]['status'] = '<font style="color:green;">是</font>';
            }else{
               $article_list[$key]['status'] = '<font style="color:red;">否</font>';
            }

            $tmp = $type_model->select(array('_id'=>new MongoId($value['typeid'])));
            $article_list[$key]['type_name'] = $tmp[0]['name'] ? $tmp[0]['name'] : $value['typeid'];
         }
         $data = array(
            'active'       => 0,
            'article_list' =>$article_list,
         );
         $this->getView()->assign($data);
      }

      public function addAction(){

         $post       = $this->getRequest()->getPost();

         $type_model = new typeModel();
         $list       = $type_model->select(array('status'=>'1'));
         foreach ($list as $key => $value) {
            $list_arr[] = $value;
         }
         $tree       = new tree($list_arr);
         $type_tree  =  $tree->get_tree_str();
         if(empty($post)){

         }else{
            $post['addtime']  = time();
            $article_model    = new articleModel();

            //查一下重 跟据title查
            $diff_res = $article_model->select(array('title'=>$post['title']));
            if(!is_null($diff_res) && $diff_res->count() > 0){

               $data['add_msg'] = '<font style="color:red;">标题相同的文章不能重复插入</font>';
            }else{
            
               $res = $article_model->insert($post);
               if(isset($res) && ($res['ok'] == '1')){
                  $data['add_msg'] = '<font style="color:green;">添加成功</font>';
               }else{
                  $data['add_msg'] = '<font style="color:red;">添加失败</font>';
               }
            }
         }

         $data['active']      = 1;
         $data['type_tree']   = $type_tree;

         $this->getView()->assign($data);

      }

      public function typeAction(){
         $type_model = new typeModel();
         $list       = $type_model->select(array('status'=>'1'));
         foreach ($list as $key => $value) {
            $list_arr[] = $value;
         }
         $tree       = new tree($list_arr);
         $type_arr   =  $tree->get_tree_arr();
         $data = array(
            'active'    => 2,
            'type_arr'  => $type_arr
         );
         $this->getView()->assign($data);
      }

      public function addtypeAction(){
         $type_model = new typeModel();
         $list       = $type_model->select(array('status'=>'1'));
         foreach ($list as $key => $value) {
            $list_arr[] = $value;
         }
         $tree       = new tree($list_arr);
         $type_tree  =  $tree->get_tree_str();
         $data = array(
            'active'    => 3,
            'type_tree' =>$type_tree,
         );
         $this->getView()->assign($data);
      }

      public function ajax_addtypechkAction(){
         $post = $this->getRequest()->getPost();
         $type_model = new typeModel();
         $res = $type_model->insert($post);
         if($res['ok'] == '1'){
            exit(json_encode(array(
                  'flag'   => true,
                  'msg'    => '添加成功'
               )));
         }
         exit(json_encode(array(
                  'flag'   => false,
                  'msg'    => '操作失败'
               )));
      }

}

?>